<template>
  <el-dialog
    v-model="visible"
    width="700px"
    :title="dynamic.dynamicTitle"
    destroy-on-close
    :before-close="handleClose"
  >
    <div class="detail-wrapper">
      <!-- 图片轮播 -->
      <div v-if="dynamic.imagesUrl?.length" class="swiper-wrapper">
        <div class="image-container" @click="openPreview(currentIndex)">
          <img :src="dynamic.imagesUrl[currentIndex]" class="big-image" />
          <div class="image-indicator">
            第 {{ currentIndex + 1 }} / {{ dynamic.imagesUrl.length }} 张
          </div>
        </div>
        <button class="prev" @click="prev" :disabled="currentIndex === 0">
          ←
        </button>
        <button
          class="next"
          @click="next"
          :disabled="currentIndex === dynamic.imagesUrl.length - 1"
        >
          →
        </button>
      </div>

      <!-- 文本内容 -->
      <h3 class="title">{{ dynamic.dynamicTitle }}</h3>
      <p class="content">{{ dynamic.dynamicContent }}</p>

      <!-- 用户信息 -->
      <div class="user-info">
        <img :src="dynamic.avatar" class="avatar" />
        <span class="username">{{ dynamic.username }}</span>
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  modelValue: Boolean,
  dynamic: Object,
})
const emit = defineEmits(['update:modelValue'])

const visible = ref(props.modelValue)
watch(
  () => props.modelValue,
  (val) => (visible.value = val)
)
watch(visible, (val) => emit('update:modelValue', val))

const currentIndex = ref(0)
const prev = () => currentIndex.value > 0 && currentIndex.value--
const next = () =>
  currentIndex.value < props.dynamic.imagesUrl.length - 1 &&
  currentIndex.value++
const handleClose = () => emit('update:modelValue', false)
</script>

<style scoped>
.detail-wrapper {
  padding: 10px;
}
.title {
  font-size: 18px;
  margin: 8px 0;
}
.content {
  font-size: 14px;
  color: #333;
}
.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
}
.username {
  font-weight: bold;
}
.image-container {
  position: relative;
}
.big-image {
  width: 100%;
  max-height: 300px;
  object-fit: contain;
  border-radius: 6px;
}
.image-indicator {
  position: absolute;
  top: 10px;
  right: 12px;
  background-color: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 12px;
}
.prev,
.next {
  position: absolute;
  top: 50%;
  background: #fff;
  border: none;
  font-size: 16px;
  cursor: pointer;
  padding: 4px 8px;
}
.prev {
  left: 10px;
}
.next {
  right: 10px;
}
</style>
